﻿// ***********************************************************************
// Assembly         : STX.Web
// Author           : seven.x
// Created          : 12-26-2013
//
// Last Modified By : seven.x
// Last Modified On : 12-30-2013
// ***********************************************************************
// <copyright file="BaseController.cs" company="驰创数码">
//     Copyright (c) 驰创数码. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Web.Mvc;
using STX.Lib.Utils;
using STX.Service;

namespace STX.Web.Controllers
{
    /// <summary>
    /// Class BaseController.
    /// </summary>
    public abstract class BaseController:Controller
    {
        /// <summary>
        /// Gets or sets the tab black list service.
        /// </summary>
        /// <value>The tab black list service.</value>
        public TabBlackListService TabBlackListService { get; set; }

        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="requestContext">HTTP 上下文和路由数据。</param>
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            if (TabBlackListService.CurrentIsInBlackIpList(Domain.TabBlackList.BlockScope.Front | Domain.TabBlackList.BlockScope.All))
            {
                requestContext.HttpContext.Response.Status = "403 Access Forbidden";
                requestContext.HttpContext.Response.StatusCode = 403;
                requestContext.HttpContext.Response.Write("<H1>403 Access Forbidden</H1>");
                requestContext.HttpContext.Response.End();
            }
            base.Initialize(requestContext);
        }

        /// <summary>
        /// 在执行由操作方法返回的操作结果前调用。
        /// </summary>
        /// <param name="filterContext">有关当前请求和操作结果的信息</param>
        protected override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var viewResult = filterContext.Result as ViewResult;
            if (viewResult != null)
            {
                var controller = filterContext.RequestContext.RouteData.Values["Controller"].ToString();
                var action = filterContext.RequestContext.RouteData.Values["Action"].ToString();
                var templateName = STXConfig.GetValue("/config/app/templates");
                if (string.IsNullOrWhiteSpace(viewResult.ViewName))
                {
                    viewResult.ViewName = string.Format(
                        "~/templates/{0}/{1}/{2}.{3}",
                        templateName,
                        controller,
                        action,
                        "cshtml");
                    return;
                }
            }
            base.OnResultExecuting(filterContext);
        }
    }
}
